package com.chis.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年06月11日
 */
public class Test105 {
    public static void main(String[] args) {

        Properties props = new Properties();
        props.put("bootstrap.servers", "10.88.88.105:9092");
        props.put("group.id", "wljgroup");
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "3000");
        props.put("request.timeout.ms", "3000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("max.poll.records",10);



        KafkaConsumer<String, Object> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList("wlj3"));

//        String topicName = "TOPIC_TEST";
////        //用于分配topic和partition
//        consumer.assign(Arrays.asList(new TopicPartition("maxwell", 0)));
//        //不改变当前offset，指定从这个topic和partition的开始位置获取。
//        consumer.seekToBeginning(Arrays.asList(new TopicPartition(topicName, 0)));
////        consumer.seek(new TopicPartition(topicName, 4),10L);

        while (true) {
            ConsumerRecords<String, Object> records = consumer.poll(1000);
            for (ConsumerRecord<String, Object> record : records){
                System.out.println("=====================================");
                System.out.printf("partition = %d, offset = %d, key = %s, value = %s", record.partition(), record.offset(), record.key(), record.value()+"\n");
            }

        }
    }
}
